<template>
    <div>
        <!--顶栏-->
        <div class="top-bar">
            <van-icon name="arrow-left" color="white" size="30" @click="goBack"/>
            <h3>{{detailsObj.articleSource}}</h3>
            <van-icon name="search" class="search" color="white" size="30" @click="toSearch"/>
        </div>
        <!--内容-->
        <div class="news-content">
            <!--标题-->
            <h3>{{this.detailsObj.title}}</h3>
            <!--内容-->
            <div class="content" v-html="this.detailsObj.content"></div>
        </div>
        <!--底栏-->
        <div class="bottom-bar">
        <!--点赞-->
            <div class="good">
                <van-icon name="good-job-o"  v-show="ifLike" color="#000" size="30" @click="getLike"/>
                <van-icon name="good-job"  v-show="!ifLike" color="#f34f4f" size="30" @click="changeLike"/>
                <span>{{this.like}}</span>
            </div>
        <!--收藏-->
            <div class="collect">
                <van-icon name="star-o" v-show="ifCollect" color="#000" size="30" @click="getCollect"/>
                <van-icon name="star" v-show="!ifCollect" color="#f34f4f" size="30" @click="changeCollect" />
                <span>{{this.collect}}</span>
            </div>
            <!--阅读量-->
            <div class="read">
                <van-icon name="browsing-history-o" color="#000" size="30"/>
                <span>{{this.readNum}}</span>
            </div>
        </div>
    </div>
</template>

<script>
import {
  getNewsDetails,
  getCollect,
  collectDetails,
  getPraise,
  addRead
} from "../api/CMS-api";
import { mapState } from "vuex";
import { ADDREAD } from "../store/mutaions_type";
export default {
  created() {
    var id = this.$route.params.articleId;
    this.historyList = JSON.parse(
      localStorage.getItem("Article_history") || "[]"
    );
    //   判重
    if (this.historyList.includes(id) == false) {
      this.historyList.unshift(id);
    }
    localStorage.setItem("Article_history", JSON.stringify(this.historyList));

    getNewsDetails(this.$route.params.articleId).then(res => {
      this.detailsObj = res.data;
    });
    collectDetails(this.$route.params.articleId).then(res => {
      if (res.data.isCollect == false) {
        this.ifCollect = true;
      } else {
        this.ifCollect = false;
        this.collect += 1;
      }
    });
    addRead(this.$route.params.articleId).then(res => {
      if (res.code == 0) {
        let num = this.readNum;
        num += 1;
        this.$store.commit(ADDREAD, {
          readNum: num
        });
      } else {
        return;
      }
    });
  },
  data() {
    return {
      ifLike: true,
      ifCollect: true,
      like: 0,
      collect: 0,
      detailsObj: {},
      collectData: {},
      historyList: []
    };
  },
  computed: {
    ...mapState({
      readNum: "readNum"
    })
  },
  methods: {
    goBack() {
      this.$router.back();
    },
    toSearch() {
      this.$router.push("/search");
    },
    getLike() {
      getPraise(this.$route.params.articleId).then(res => {
        console.log(res);
        if (res.code == 0) {
          this.ifLike = false;
          this.$notify({
            type: "success",
            message: "点赞成功",
            background: "#f7f4f4",
            color: "#000"
          });
          this.like += 1;
        } else {
          this.$toast.fail("请先登录");
        }
      });
    },
    changeLike() {
      this.$dialog
        .confirm({
          title: "取消点赞",
          message: "是否确认取消点赞",
          confirmButtonColor: "red"
        })
        .then(() => {
          getPraise(this.$route.params.articleId).then(res => {
            console.log(res);
            this.ifLike = true;
            this.like -= 1;
          });
        })
        .catch(() => {
          this.ifLike = false;
        });
    },
    getCollect() {
      getCollect(this.$route.params.articleId).then(res => {
        console.log(res);
        if (res.code == 0) {
          this.ifCollect = false;
          this.$notify({
            type: "success",
            message: "收藏成功",
            background: "#f7f4f4",
            color: "#000"
          });
          this.collect += 1;
        } else {
          this.$toast.fail("请先登录");
        }
      });
    },
    changeCollect() {
      this.$dialog
        .confirm({
          title: "取消收藏",
          message: "是否确认取消收藏",
          confirmButtonColor: "red"
        })
        .then(() => {
          getCollect(this.$route.params.articleId).then(res => {
            console.log(res);
            this.ifCollect = true;
            this.collect -= 1;
          });
        })
        .catch(() => {
          this.ifCollect = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
    .top-bar {
        width: 100%;
        height: 50px;
        background-color: #f34f4f;
        display: flex;
        justify-content: space-between;
        align-items:center;
        h3 {
            color: white;
            font-size: 23px;
            font-family: "\5B8B\4F53"
        }
        .search {
            margin-right: 10px;
        }
    }
    .news-content {
        margin: 20px 20px 60px 20px;
        h3 {
            text-align: center;
        }
        .content {
            font-size: 15px;
            /deep/ p {
                display: block;
                margin-block-start: 1em;
                margin-block-end: 1em;
                text-indent:2em
            }
            /deep/ .photo {
                img {
                    width: 100%;
                }
            }
            /deep/ .art_img_mini {
                img {
                    width: 100%;
                }
            }
            /deep/ .art_p {
                img {
                    width: 100%;
                    margin-left: -27px;
                }
            }
        }
    }
    .bottom-bar {
        width: 100%;
        /*margin-left: -27px;*/
      }
.bottomBar {
  width: 100%;
  height: 60px;
  background-color: #f7f4f4;
  position: fixed;
  bottom: 0;
  display: flex;
  align-items: center;
  .good {
    position: absolute;
    left: 20px;
  }
  .collect {
    position: absolute;
    left: 80px;
  }
  .read {
    position: absolute;
    right: 30px;
  }
  span {
    color: #000;
  }
}
</style>